﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using System.Web;

namespace Ditto.Models
{
    [Bind(Include = "TimeCreated,TransactionRating")]
    [MetadataType(typeof(Transaction_Validation))]
    public partial class Transaction
    {
        public bool IsTransactingUser(string transactingUserID)
        {
            return String.Equals(this.TransactingUserID, transactingUserID, StringComparison.OrdinalIgnoreCase);
        }

        public bool IsPostingUser(string postingUserID)
        {
            return String.Equals(this.Item.PostingUserID, postingUserID, StringComparison.OrdinalIgnoreCase);
        }

        public bool HasFeedbackByTransactingUser()
        {
            CommentRepository commentRepository = new CommentRepository();
            var feedback = commentRepository.FindTransactionFeedbackByTransactingUser(this.TransactionID, this.TransactingUserID);

            return feedback != null;
        }

        public bool HasFeedbackByPostingUser()
        {
            CommentRepository commentRepository = new CommentRepository();
            var feedback = commentRepository.FindTransactionFeedbackByPostingUser(this.TransactionID, this.TransactingUserID);

            return feedback != null;
        }

        [UIHint("LocationDetail")]
        public LocationDetail PickupLocation
        {
            get
            {
                return new LocationDetail() { Latitude = this.PickupLatitude ?? 0, Longitude = this.PickupLongitude ?? 0, Title = this.Item.Title, Address = this.PickupAddress };
            }
            set
            {
                this.PickupLatitude = value.Latitude;
                this.PickupLongitude = value.Longitude;
                this.Item.Title = value.Title;
                this.PickupAddress = value.Address;
            }
        }

        [UIHint("LocationDetail")]
        public LocationDetail ReturnLocation
        {
            get
            {
                return new LocationDetail() { Latitude = this.ReturnLatitude ?? 0, Longitude = this.ReturnLongitude ?? 0, Title = this.Item.Title, Address = this.ReturnAddress };
            }
            set
            {
                this.ReturnLatitude = value.Latitude;
                this.ReturnLongitude = value.Longitude;
                this.Item.Title = value.Title;
                this.ReturnAddress = value.Address;
            }
        }

    }

    public class Transaction_Validation
    {
        [HiddenInput(DisplayValue = false)]
        public int ItemID { get; set; }

        [HiddenInput(DisplayValue = false)]
        [StringLength(256, ErrorMessage = "Hosted By name may not be longer than 20 characters")]
        public string TransactingUserID { get; set; }

        [Display(Name = "Pick up item at: ")]
        [Required(ErrorMessage = "Address is required")]
        [StringLength(50, ErrorMessage = "Address may not be longer than 50 characters")]
        public string PickupAddress { get; set; }

        [HiddenInput(DisplayValue = false)]
        public double PickupLatitude { get; set; }

        [HiddenInput(DisplayValue = false)]
        public double PickupLongitude { get; set; }

        [Display(Name = "Picked up? ")]
        public bool IsPickedup { get; set; }

        [Display(Name = "Return item at: ")]
        [Required(ErrorMessage = "Address is required")]
        [StringLength(50, ErrorMessage = "Address may not be longer than 50 characters")]
        public string ReturnAddress { get; set; }

        [HiddenInput(DisplayValue = false)]
        public double ReturnLatitude { get; set; }

        [HiddenInput(DisplayValue = false)]
        public double ReturnLongitude { get; set; }

        [Display(Name = "Returned? ")]
        public bool IsReturned { get; set; }

        [Display(Name = "Transaction Completed? ")]
        public bool IsCompleted { get; set; }

        [HiddenInput(DisplayValue = false)]
        public DateTime TimeCompleted { get; set; }

        [HiddenInput(DisplayValue = false)]
        public double TransactionRating { get; set; }

        [HiddenInput(DisplayValue = false)]
        public DateTime TimeCreated { get; set; }
    }

}